package com.tengfei.api.user;

import cn.hutool.core.util.ObjectUtil;
import com.tengfei.common.annotation.Anonymous;
import com.tengfei.common.core.domain.R;
import com.tengfei.main.system.domain.SysDictData;
import com.tengfei.main.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author feige
 * @version v1.0
 * @date 2022-08-18-17:35
 * @description 用户工具接口
 */

@RestController
@Api(value = "用户工具接口控制器", tags = {"用户工具接口模块"})
@RequestMapping("user/utils")
public class UserUtilsController {

    @Resource
    private ISysDictTypeService dictTypeService;

    /**
     * 根据字典类型查询字典数据信息
     */
    @Anonymous
    @ApiOperation("查询字典数据信息(根据字典类型)")
    @GetMapping(value = "/dictValue/{dictType}")
    public R<List<SysDictData>> dictType(@ApiParam("字典类型") @PathVariable String dictType) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (ObjectUtil.isNull(data)) {
            data = new ArrayList<>();
        }
        return R.ok(data);
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @Anonymous
    @ApiOperation("测试HttpServletRequest")
    @GetMapping()
    public R<String> dictType(HttpServletRequest request) {
        String ip=request.getHeader("x-forwarded-for");
        if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)){
            ip=request.getHeader("Proxy-Client-IP");
        }
        if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)){
            ip=request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)){
            ip=request.getHeader("X-Real-IP");
        }
        if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)){
            ip=request.getRemoteAddr();
        }
        return R.ok(ip);
    }

}
